package Fragment;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import Database.SQLquery;
import ListView_Adapter.StudentList_Adapter;
import ListView_Adapter.Student_Detail;
import barcode.code.nhat.iuhbarcode.Main_Action;
import barcode.code.nhat.iuhbarcode.R;

import static barcode.code.nhat.iuhbarcode.R.layout.fragment_offline;

public class Create_Fragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";

    public static Create_Fragment newInstance(int sectionNumber) {
        Create_Fragment fragment = new Create_Fragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public Create_Fragment() {
    }


    private EditText e1, e2, e3, e4;
    private Button b1;
    private SQLquery login;
    private ListView lv;
    private StudentList_Adapter kid;
    public ArrayList<Student_Detail> mAdapter = new ArrayList<Student_Detail>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(fragment_offline, container, false);
        login = new SQLquery(getActivity());
        login = login.open();

        e1 = (EditText) v.findViewById(R.id.ed_fname);
        e2 = (EditText) v.findViewById(R.id.ed_lname);
        e3 = (EditText) v.findViewById(R.id.ed_id);
        e4 = (EditText) v.findViewById(R.id.ed_cl);
        b1 = (Button) v.findViewById(R.id.bt_student_create);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String fname = String.valueOf(e1.getText());
                String lname = String.valueOf(e2.getText());
                String id = String.valueOf(e3.getText());
                String class_name = String.valueOf(e4.getText());
                if (fname.equals("") && id.equals("")) {
                    Toast.makeText(getActivity(), "Check again and fill form!", Toast.LENGTH_SHORT).show();
                } else {

                    if (login.check_id(id)) {
                        login.insert_student(id, fname, lname, class_name);
                        Toast.makeText(getActivity(), "Success!", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getActivity(), "ID is exists!", Toast.LENGTH_SHORT).show();
                    }
                }

            }
        });

        lv = (ListView) v.findViewById(R.id.list_student);
        List<Student_Detail> stu = login.get_list_student();
        for (Student_Detail cn : stu) {
            mAdapter.add(cn);
        }
        kid = new StudentList_Adapter(getActivity(), mAdapter);
        lv.setAdapter(kid);
        return v;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((Main_Action) activity).onSectionAttached(getArguments().getInt(ARG_SECTION_NUMBER));
    }
}
